
#include "stdafx.h"

TEST(connectTest, connect_in_inout)
{
    SOCKET sock = INVALID_SOCKET;
    sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    addr.sin_port = htons(27015);
    int len = 11;
    
    Header header(GetCurrentThreadId(), "connect");
    connect_in_inout connectIN(sock, (SOCKADDR*)&addr, len);
    connect_in_inout connectOUT;

    std::vector<char> buffer;
    Serialize(header, connectIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(connectOUT);

    EXPECT_EQ (header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ (header.func_name_, deserialize.get_header().func_name_);

    EXPECT_EQ (connectIN.s_, connectOUT.s_);
    if (connectIN.addr_)
    {
        EXPECT_EQ (connectIN.addr_->sa_family, connectOUT.addr_->sa_family);
        int cmp = strncmp (connectIN.addr_->sa_data, connectOUT.addr_->sa_data, 14);
        EXPECT_TRUE (!cmp);
    }
    EXPECT_EQ (connectIN.addrlen_, connectOUT.addrlen_);
}

TEST(connectTest, connect_in_inout_NULL_parameters)
{
    SOCKET sock = INVALID_SOCKET;
    int len = 11;
    
    Header header(GetCurrentThreadId(), "connect");
    connect_in_inout connectIN(sock, NULL, len);
    connect_in_inout connectOUT;

    std::vector<char> buffer;
    Serialize(header, connectIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(connectOUT);

    EXPECT_EQ (header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ (header.func_name_, deserialize.get_header().func_name_);

    EXPECT_EQ (connectIN.s_, connectOUT.s_);
    if (connectIN.addr_)
    {
        EXPECT_EQ (connectIN.addr_->sa_family, connectOUT.addr_->sa_family);
        int cmp = strncmp (connectIN.addr_->sa_data, connectOUT.addr_->sa_data, 14);
        EXPECT_TRUE (!cmp);
    }
    EXPECT_EQ (connectIN.addrlen_, connectOUT.addrlen_);
}

TEST(connectTest, connect_out_inout)
{
    SOCKET sock = INVALID_SOCKET;
    
    Header header(GetCurrentThreadId(), "connect");
    connect_out_inout connectIN(7);
    connect_out_inout connectOUT;

    std::vector<char> buffer;
    Serialize(header, connectIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(connectOUT);

    EXPECT_EQ (header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ (header.func_name_, deserialize.get_header().func_name_);

    EXPECT_EQ (connectIN.wsa_last_error_, connectOUT.wsa_last_error_);
}

//TEST(connectTest, connect_out_inout_NULL_parameters)
//{
//    
//}
